/*
* 生产模式，功能：
* 1. css添加前缀
* 2. sass编译
* 3. es6编译
* 4. js，css，图片压缩
* 5. 路径替换
*/
const color = require('colors-cli/safe')
const error = color.red_bt
const warn = color.yellow
const notice = color.cyan
const tell = color.green_bt
const Config = require('./gulp.config.js')
const serverConfig = require('./server.config.js')
const gulp = require('gulp')
const del = require('del')
const base64 = require('gulp-base64')
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const sass = require('gulp-sass')
const imagemin = require('gulp-imagemin')
const imageminOptipng = require('imagemin-optipng')
const pngquant = require('imagemin-pngquant')
const imageminJpegRecompress = require('imagemin-jpeg-recompress')
const smushit = require('gulp-smushit');
const babel = require('gulp-babel')
const uglify = require('gulp-uglify')
const rename = require('gulp-rename')
const cleanCSS = require('gulp-clean-css')
const replace = require('gulp-replace')

const processors = [
  autoprefixer({browsers: ['last 20 version']})
]
function build () {
  gulp.task('clean', function(){
    return del([
        Config.dist + '/**'
    ])
  })
  gulp.task('html', function () {
    console.log(notice('----已清理原生产环境----'));
    return gulp.src(Config.html.src, {base: Config.src})
      .pipe(gulp.dest(Config.dist))
  })
  gulp.task('replace', ['html'], function () {
    let publicPath = serverConfig.publicPath;
    if(publicPath != ''){
      console.log(warn('----开始替换服务器资源路径----'));
      return gulp.src([Config.dist + '**/*.html'], {base: Config.src})
        .pipe(replace(/(\.\/static\/)|(static\/)/g, publicPath))
        .pipe(gulp.dest(Config.dist))
    }
    console.log(warn('----不存在服务器资源路径，跳过替换任务----'));
    return
  })
  gulp.task('css', function () {
    return gulp.src(Config.css.src, {base: Config.src})
      .pipe(base64(Config.base64))
      .pipe(postcss(processors))
      // .pipe(cleanCSS())
      .pipe(gulp.dest(Config.dist))
  })
  gulp.task('sass', function () {
    return gulp.src(Config.sass.src, {base: Config.src})
      .pipe(sass().on('error', sass.logError))
      .pipe(base64(Config.base64))
      .pipe(postcss(processors))
      // .pipe(cleanCSS())
      .pipe(gulp.dest(Config.dist))
  })
  gulp.task('img', function () {
    var jpgmin = imageminJpegRecompress({
      accurate: true,//高精度模式
      quality: "high",//图像质量:low, medium, high and veryhigh;
      method: "smallfry",//网格优化:mpe, ssim, ms-ssim and smallfry;
      min: 70,//最低质量
      loops: 6,//循环尝试次数, 默认为6;
      progressive: false,//基线优化
      subsample: "default"//子采样:default, disable;
    })
    var pngmin = imageminOptipng({
      optimizationLevel: 3
    })
    return gulp.src(Config.img.src, {base: Config.src})
      .pipe(imagemin({
        use: [jpgmin(), pngquant(), pngmin()]
      }))
      .pipe(gulp.dest(Config.dist))
  })
  gulp.task('img:tiny', function () {
    return gulp.src(Config.img.src, {base: Config.src})
      .pipe(tinypng({
        key: 'API_KEY',
        sigFile: 'images/.tinypng-sigs',
        log: true
      }))
      .pipe(gulp.dest(Config.dist))
  })
  gulp.task('js', function () {
    return gulp.src(Config.js.src, {base: Config.src})
      .pipe(babel({
        presets: ['es2015']
      }))
      // .pipe(uglify())
      .pipe(gulp.dest(Config.dist))
  })
  gulp.task('libs', function () {
    return gulp.src(Config.libs.src, {base: Config.src})
      .pipe(gulp.dest(Config.dist))
  })
  gulp.task('build', ['clean'], function () {
    gulp.start('final');
  })
  gulp.task('final', ['libs', 'js', 'img', 'css', 'sass', 'replace'], function () {
    console.log(notice("----已成功处理所有文件，请检查一遍----"))
  })
}

module.exports = build